package com.xjh.serverinspect.security;

import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 密码工具
 * </p>
 *
 * @author xujinghui
 * @since 2024-12-11
 */
public class PasswordUtils {

    private static final String ENC_KEY = "aK78CfgZ29QdS4rW";
    private static final byte[] prefix = new byte[]{115, 118, 105, 112, 95};

    public static boolean isEncrypted(String password) {
        return password != null && password.startsWith(new String(prefix, StandardCharsets.UTF_8));
    }

    public static String encrypt(String password) {
        if (!StringUtils.isEmpty(password) && !isEncrypted(password)) {
            return new String(prefix, StandardCharsets.UTF_8) + AESUtils.encrypt(password, ENC_KEY);
        }
        return password;
    }

    public static String decrypt(String password) {
        if (isEncrypted(password)) {
            return AESUtils.decrypt(password.substring(5), ENC_KEY);
        }
        return password;
    }

}
